package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.OrderDetailetDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.util.DBUtil;
public class OrderDetailetDaoImpl implements OrderDetailetDao{
 
 private Connection con = null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 
 @Override
 public int saveOrderDetailetBatch(List<OrderDetailet> list) throws Exception {
     int result = 0;
     StringBuffer stringBuffer = new StringBuffer("insert into orderDetailet(orderId,foodId,quantity,food_name,food_price,food_img,food_explain) values");
     for(OrderDetailet od : list) {
         stringBuffer.append("("+od.getOrderId()+","+od.getFoodId()+","+od.getQuantity()+",'"+od.getFoodName()+"',"+od.getFoodPrice()+",'"+od.getFoodImg()+"','"+od.getFoodExplain()+"'),");
     }
     String sql = stringBuffer.toString().substring(0,stringBuffer.toString().length()-1);
     try {
         con = DBUtil.getConnection();
         pst = con.prepareStatement(sql);
         result = pst.executeUpdate();
     } finally {
         DBUtil.close(pst);
     }
     return result;
 }

@Override
public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception{
List<OrderDetailet> list = new ArrayList<>();

StringBuffer sql = new StringBuffer();
sql.append(" select o.* from orderdetailet o where o.orderId=?");

try {
con = DBUtil.getConnection();
pst = con.prepareStatement(sql.toString());
pst.setInt(1, orderId);
rs = pst.executeQuery();
while(rs.next()) {
	OrderDetailet od = new OrderDetailet();
	od.setOdId(rs.getInt("odId"));
	od.setOrderId(rs.getInt("orderId"));
	od.setFoodId(rs.getInt("foodId"));
	od.setQuantity(rs.getInt("quantity"));
	od.setFoodName(rs.getString("food_name"));
	od.setFoodPrice(rs.getBigDecimal("food_price"));
	od.setFoodImg(rs.getString("food_img"));
	od.setFoodExplain(rs.getString("food_explain"));

	list.add(od);
}
}finally {
DBUtil.close(pst);
}
return list;
}
}
